<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-header">
        <img src="@/assets/logo.svg" alt="Logo" class="login-logo" />
        <h2 class="login-title">登录到 <span class="text-primary">NexusHub</span></h2>
        <p class="login-subtitle">智能一体化管理系统</p>
      </div>
      
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            prefix-icon="el-icon-user"
            clearable
            @keyup.enter="handleLogin"
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            show-password
            @keyup.enter="handleLogin"
          />
        </el-form-item>
        
        <el-form-item prop="captcha">
          <el-input
            v-model="loginForm.captcha"
            placeholder="请输入验证码"
            prefix-icon="el-icon-key"
            @keyup.enter="handleLogin"
          />
        </el-form-item>
        
        <div class="login-options">
          <el-checkbox v-model="loginForm.rememberMe">记住我</el-checkbox>
          <el-button type="text" @click="handleForgotPassword">忘记密码?</el-button>
        </div>
        
        <el-button type="primary" :loading="loading" class="login-button" @click="handleLogin">
          登录
        </el-button>
        
        <div class="login-divider">
          <span>或使用以下方式登录</span>
        </div>
        
        <div class="social-login">
          <el-button circle class="social-button wechat" title="企业微信登录">
            <i class="el-icon-wechat"></i>
          </el-button>
          <el-button circle class="social-button dingtalk" title="钉钉登录">
            <i class="el-icon-chat-dot-square"></i>
          </el-button>
          <el-button circle class="social-button alipay" title="支付宝登录">
            <i class="el-icon-wallet"></i>
          </el-button>
        </div>
        
        <div class="register-link">
          还没有账号? <el-button type="text" @click="handleRegister">注册</el-button>
        </div>
      </el-form>
    </div>
    
    <div class="login-background"></div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { login, getCaptcha } from '@/api/auth' // 导入 login 和 getCaptcha API

const router = useRouter()
const loading = ref(false)

// 验证码相关状态
const captchaImage = ref('')
const captchaId = ref('')

// 登录表单
const loginForm = reactive({
  username: 'admin',
  password: '123456',
  rememberMe: false,
  captcha: '', // 添加验证码字段
  captchaId: '' // 添加验证码ID字段
})

// 表单验证规则
const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ],
  captcha: [ // 添加验证码验证规则
    { required: true, message: '请输入验证码', trigger: 'blur' },
    { len: 4, message: '验证码长度为4位', trigger: 'blur' } // 假设验证码是4位
  ]
}

// 获取验证码图片
const getCaptchaImage = async () => {
  try {
    const res = await getCaptcha()
    captchaImage.value = res.data.captchaImage
    captchaId.value = res.data.captchaId
    loginForm.captchaId = res.data.captchaId // 将captchaId赋值给表单
  } catch (error) {
    ElMessage.error('获取验证码失败')
    console.error('获取验证码失败', error)
  }
}

// 处理登录
const handleLogin = async () => {
  loading.value = true
  try {
    await loginForm.value.validate() // 触发表单验证
    const res = await login({
      username: loginForm.username,
      password: loginForm.password,
      captcha: loginForm.captcha,
      captchaId: loginForm.captchaId
    })
    
    // 保存token到本地存储
    localStorage.setItem('token', res.data.token)
    localStorage.setItem('refreshToken', res.data.refreshToken)
    localStorage.setItem('tokenExpireTime', res.data.tokenExpireTime)
    
    // 显示登录成功消息
    ElMessage.success('登录成功')
    
    // 跳转到首页
    router.push('/')
  } catch (error) {
    ElMessage.error(error.msg || '登录失败')
    console.error('登录失败', error)
    getCaptchaImage() // 登录失败时刷新验证码
  } finally {
    loading.value = false
  }
}

// 处理忘记密码
const handleForgotPassword = () => {
  ElMessage.info('忘记密码功能开发中...')
}

// 处理注册
const handleRegister = () => {
  ElMessage.info('注册功能开发中...')
}

// 组件挂载时获取验证码
onMounted(() => {
  getCaptchaImage()
})
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f8fc;
  position: relative;
  overflow: hidden;
}

.login-card {
  width: 420px;
  padding: 40px;
  background: white;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(62, 120, 178, 0.1);
  position: relative;
  z-index: 2;
}

.login-background {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #3E78B2, #4ECDC4);
  clip-path: polygon(0 0, 100% 0, 100% 65%, 0 100%);
  z-index: 1;
  opacity: 0.1;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-logo {
  width: 180px;
  margin-bottom: 16px;
}

.login-title {
  font-size: 24px;
  font-weight: 600;
  margin-bottom: 8px;
  color: #1A2634;
}

.text-primary {
  color: #3E78B2;
}

.login-subtitle {
  font-size: 14px;
  color: #666;
  margin: 0;
}

.login-form {
  margin-top: 24px;
}

.login-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.login-button {
  width: 100%;
  height: 44px;
  font-size: 16px;
  background: linear-gradient(135deg, #3E78B2, #4ECDC4);
  border: none;
}

.login-divider {
  display: flex;
  align-items: center;
  margin: 24px 0;
  color: #999;
  font-size: 14px;
}

.login-divider::before,
.login-divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background-color: #eee;
}

.login-divider span {
  padding: 0 16px;
}

.social-login {
  display: flex;
  justify-content: center;
  margin-bottom: 24px;
}

.social-button {
  width: 44px;
  height: 44px;
  margin: 0 8px;
  font-size: 20px;
  border: 1px solid #eee;
  background-color: #fff;
}

.social-button.wechat {
  color: #07C160;
}

.social-button.dingtalk {
  color: #1890FF;
}

.social-button.alipay {
  color: #1677FF;
}

.social-button:hover {
  transform: translateY(-3px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.register-link {
  text-align: center;
  font-size: 14px;
  color: #666;
}
</style> 